package com.wuchao.controller;

import com.github.pagehelper.PageHelper;
import com.wuchao.entity.User;
import com.wuchao.service.serviceImpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2022/7/7 10:20
 * @Version 1.0
 */
@WebServlet(name = "ServleUser",urlPatterns = "/ServleUser")
public class ServleUser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //out对象
        PrintWriter out = response.getWriter();//文件流
        //刷新缓存区 关闭
        out.flush();
        out.close();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //乱码问题
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //out对象
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");//那到选项
        ServiceImpl service = new ServiceImpl();
        if(op!=null){//有选项
            if(op.equals("delete")){//删除
                int id=Integer.parseInt(request.getParameter("id"));
                int update = service.update("delete from user where id=?", id);
                if(update>0){//成功
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isdelete","删除成功");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }else{//失败
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isdelete","删除失败");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }
            }else  if(op.equals("deleteajax")){//异步删除
                int id=Integer.parseInt(request.getParameter("id"));
                System.out.println(id);
                int update = service.update("delete from user where id=?", id);
                if(update>0){//成功
                    out.print("yes");
                }else{//失败
                    out.print("no");
                }
            }
            else if(op.equals("update1")){//修改
                int id=Integer.parseInt(request.getParameter("id"));
                List<User> poJo = service.findPoJo("select * from user where id=?",id);
                request.setAttribute("list",poJo);
                request.getRequestDispatcher("/jstlnc/update.jsp").forward(request,response);
            }else if(op.equals("update2")){//修改
                int id=Integer.parseInt(request.getParameter("id"));
                String name=request.getParameter("name");
                int sex=Integer.parseInt(request.getParameter("sex"));
                String hobby=request.getParameter("hobby");
                int update = service.update("update user set name=?,sex=?,hobby=? where id=?", name, sex, hobby, id);
                if(update>0){//修改成功
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isupdate","修改成功");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }else{//失败
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isupdate","修改失败");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }
            }else if(op.equals("add")){//增加
                String name=request.getParameter("name");
                int sex=Integer.parseInt(request.getParameter("sex"));
                String hobby=request.getParameter("hobby");
                int update = service.update("insert user(name,sex,hobby) values(?,?,?)", name, sex, hobby);
                if(update>0){//增加成功
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isadd","增加成功");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }else{//失败
                    List<User> poJo = service.findPoJo("select * from user");//拿到结果集
                    //?带不过泛型集合 只能属性设置
                    request.setAttribute("list",poJo);
                    request.setAttribute("isadd","增加失败");
                    request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
                }
            }

        }else{//第一次来
            List<User> poJo = service.findPoJo("select * from user");//拿到结果集
            //?带不过泛型集合 只能属性设置
            request.setAttribute("list",poJo);
            //request.getRequestDispatcher("/jstlnc/main.jsp").forward(request,response);
            request.getRequestDispatcher("/pageinfo/page.jsp").forward(request,response);
        }




        out.flush();
        out.close();

    }

}
